﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

using System.Reflection;
using System.IO;

using mbine.msgc.model;
using mbine.msgc.tool;
using mbine.msgc.controls;
using System.Threading;

namespace mbine.msgc.forms
{
    /// <summary>
    /// Interaction logic for Chat.xaml
    /// </summary>
    public partial class Chat : IRoom
    {
        public string Key { get; set; }                 //현재 이 방의 고유키
        public string CommonKey { get; set; }           //대화자들간의 연결되는 고유키
        public string FormId { get; set; }
        public DateTime MakeTime { get; set; }
        public List<IUser> AllMembers { get; set; }
        public List<IUser> Members { get; set; }
        public CThread Thread { get; set; }
        
        public Chat()
        {
            InitializeComponent();

            InitControl();

            Thread = new CThread();
            this.Key = CTools.MakeRoomKey();
            this.CommonKey = Key;
            Init();
        }

        public Chat(string _commonKey)
        {
            InitializeComponent();

            InitControl();

            Thread = new CThread();
            this.CommonKey = _commonKey;
            this.Key = CTools.MakeRoomKey();
            Init();
        }

        public Chat(CThread Thread)
        {
            InitializeComponent();

            InitControl();

            this.Key = CTools.MakeRoomKey();
            this.CommonKey = Key;
            Init();
        }

        public void Add(IUser _user)
        {
            Members.Add(_user);
            AllMembers.Add(_user);
        }

        public void Add(List<IUser> _users)
        {
            foreach (IUser user in _users)
                Add(user);
        }

        public void Remove(IUser _user)
        {
            Members.Remove(_user);
        }

        public void GetMessage(CRelayMessage msg)
        {
            Dispatcher.Invoke(new ThreadStart(() =>
            {
                uCChat.GetMessage(msg);
            }));
        }

        #region Private Methods
        private void Init()
        {
            Members = new List<IUser>();
            AllMembers = new List<IUser>();
            MakeTime = DateTime.Now;

            uCChat.Thread = Thread;
        }
        
        private void InitControl()
        {
            this.ResizeMode = System.Windows.ResizeMode.CanResizeWithGrip;
            this.WindowStyle = System.Windows.WindowStyle.None;
            this.AllowsTransparency = true;
            this.BorderThickness = new Thickness(3);
            this.BorderBrush = new SolidColorBrush(Colors.Black);

            uCChat.EventChatSend += new EventHandler(uCChat_EventChatSend);
        }

        void uCChat_EventChatSend(object sender, EventArgs e)
        {
            CRelayMessage SendData = new CRelayMessage
            {
                data = uCChat.ChatData,
                Members = Thread.Members,
                mode = "chat",
                roomkey = Thread.Key,
                sender = CCUser.Instance.Email,
                sentdate = DateTime.Now
            };

            CConnection.Instance.Send(CChatMessage.Chat("chat", CCUser.Instance.Email
                , (from p in Members select p.Email).ToList()
                , uCChat.ChatData
                , CommonKey
                , Key)
            );

            GetMessage(SendData);
        }
        #endregion
        
        private void Top_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            CChatContainer.Instance.Chats.Remove(this);
        }
    }
}
